package com.verykim.video;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class AdapterServlet extends HttpServlet {

	final static String INDEX = "^/$";
	final static String INDEX_PREV = "^/prev$";
	final static String INDEX_NEXT = "^/next$";
	final static String TODAY = "^/today$";
	final static String TONIGHT = "^/tonight$";
	// final static String TODAY_PREV = "^/today/prev/\\d{13}$";
	// final static String TODAY_NEXT = "^/today/next/\\d{13}$";
	final static String CATEGORY_VIDEO = "^/category/(%[0-9a-zA-Z]{2}){9}/video/[\\w\\W]*$";
	final static String CATEGORY = "^/category/(%[0-9a-zA-Z]{2}){9}$";
	final static String CATEGORY_PREV = "^/category/(%[0-9a-zA-Z]{2}){9}/prev$";// /\\d{13}
	final static String CATEGORY_NEXT = "^/category/(%[0-9a-zA-Z]{2}){9}/next$";
	final static String CATEGORY_YEAR = "^/category/(%[0-9a-zA-Z]{2}){9}/year/\\d{4}$";
	final static String CATEGORY_YEAR_PREV = "^/category/(%[0-9a-zA-Z]{2}){9}/year/\\d{4}/prev$";
	final static String CATEGORY_YEAR_NEXT = "^/category/(%[0-9a-zA-Z]{2}){9}/year/\\d{4}/next$";
	final static String CATEGORY_AREA = "^/category/(%[0-9a-zA-Z]{2}){9}/area/(%[0-9a-zA-Z]{2}){6,}$";
	final static String CATEGORY_AREA_PREV = "^/category/(%[0-9a-zA-Z]{2}){9}/area/(%[0-9a-zA-Z]{2}){6,}/prev$";
	final static String CATEGORY_AREA_NEXT = "^/category/(%[0-9a-zA-Z]{2}){9}/area/(%[0-9a-zA-Z]{2}){6,}/next$";
	final static String YEAR = "^/year/\\d{4}$";
	final static String YEAR_PREV = "^/year/\\d{4}/prev$";
	final static String YEAR_NEXT = "^/year/\\d{4}/next$";
	final static String AREA = "^/area/(%[0-9a-zA-Z]{2}){6,}$";
	final static String AREA_PREV = "^/area/(%[0-9a-zA-Z]{2}){6,}/prev$";
	final static String AREA_NEXT = "^/area/(%[0-9a-zA-Z]{2}){6,}/next$";
	final static String SEARCH = "^/search$";

	final static String VIDEO_EDIT = "^/edit/(%[0-9a-zA-Z]{2}){9}/video/[\\w\\W]*$";
	final static String VIDEO_DELETE = "^/delete/(%[0-9a-zA-Z]{2}){9}/video/[\\w\\W]*$";

	@Override
	public void doGet(final HttpServletRequest req,
			final HttpServletResponse resp) throws IOException {

		final int port = req.getServerPort();
		final String basePath = req.getScheme() + "://" + req.getServerName()
				+ (80 == port ? "" : ":" + port) + "/";

		final String URI = req.getRequestURI();
		final HtmlRender hr = new HtmlRender(basePath);

		if (URI.matches(INDEX)) {
			hr.IndexPage(null);
		} else if (URI.matches(TODAY)) {
			hr.TodayPage();
		} else if (URI.matches(TONIGHT)) {
			hr.TonightPage();
		} else if (URI.matches(INDEX_PREV)) {
			final String beginDate = "-" + req.getParameter("i");
			hr.IndexPage(beginDate);
		} else if (URI.matches(INDEX_NEXT)) {
			final String beginDate = req.getParameter("i");
			hr.IndexPage(beginDate);
		} else if (URI.matches(CATEGORY)) {
			final String category = URI.substring(10);
			hr.CategoryPage(category, null, null, null);
		} else if (URI.matches(CATEGORY_PREV)) {
			final int lastIndexOf = URI.lastIndexOf("/prev");
			final String category = URI.substring(10, lastIndexOf);
			final String beginDate = "-" + req.getParameter("i");
			hr.CategoryPage(category, null, null, beginDate);
		} else if (URI.matches(CATEGORY_NEXT)) {
			final int lastIndexOf = URI.lastIndexOf("/next");
			final String category = URI.substring(10, lastIndexOf);
			final String beginDate = req.getParameter("i");// URI.substring(lastIndexOf
															// + 6);
			hr.CategoryPage(category, null, null, beginDate);
		} else if (URI.matches(CATEGORY_YEAR)) {
			final int lastIndexOfyear = URI.lastIndexOf("/year/");
			final String category = URI.substring(10, lastIndexOfyear);
			final String year = URI.substring(lastIndexOfyear + 6);
			hr.CategoryPage(category, year, null, null);
		} else if (URI.matches(CATEGORY_YEAR_PREV)) {
			final int lastIndexOfyear = URI.lastIndexOf("/year/");
			final int lastIndexOfprev = URI.lastIndexOf("/prev");
			final String category = URI.substring(10, lastIndexOfyear);
			final String year = URI.substring(lastIndexOfyear + 6,
					lastIndexOfprev);
			final String beginDate = "-" + req.getParameter("i");
			hr.CategoryPage(category, year, null, beginDate);
		} else if (URI.matches(CATEGORY_YEAR_NEXT)) {
			final int lastIndexOfyear = URI.lastIndexOf("/year/");
			final int lastIndexOfnext = URI.lastIndexOf("/next");
			final String category = URI.substring(10, lastIndexOfyear);
			final String year = URI.substring(lastIndexOfyear + 6,
					lastIndexOfnext);
			final String beginDate = req.getParameter("i");
			hr.CategoryPage(category, year, null, beginDate);
		} else if (URI.matches(CATEGORY_AREA)) {
			final int lastIndexOfarea = URI.lastIndexOf("/area/");
			final String category = URI.substring(10, lastIndexOfarea);
			final String area = URI.substring(lastIndexOfarea + 6);
			hr.CategoryPage(category, null, area, null);
		} else if (URI.matches(CATEGORY_AREA_PREV)) {
			final int lastIndexOfarea = URI.lastIndexOf("/area/");
			final int lastIndexOfprev = URI.lastIndexOf("/prev");
			final String category = URI.substring(10, lastIndexOfarea);
			final String area = URI.substring(lastIndexOfarea + 6,
					lastIndexOfprev);
			final String beginDate = "-" + req.getParameter("i");// URI.substring(lastIndexOfprev
																	// + 6);
			hr.CategoryPage(category, null, area, beginDate);
		} else if (URI.matches(CATEGORY_AREA_NEXT)) {
			final int lastIndexOfarea = URI.lastIndexOf("/area/");
			final int lastIndexOfnext = URI.lastIndexOf("/next");
			final String category = URI.substring(10, lastIndexOfarea);
			final String area = URI.substring(lastIndexOfarea + 6,
					lastIndexOfnext);
			final String beginDate = req.getParameter("i");// URI.substring(lastIndexOfnext
															// + 6);
			hr.CategoryPage(category, null, area, beginDate);
		} else if (URI.matches(CATEGORY_VIDEO)) {
			final int lastIndexOf = URI.lastIndexOf("/video/");
			final String category = URI.substring(10, lastIndexOf);
			final String title = URI.substring(lastIndexOf + 7);
			hr.VideoPage(category, title);
		} else if (URI.matches(YEAR)) {
			final String year = URI.substring(6);
			hr.CategoryPage(null, year, null, null);
		} else if (URI.matches(YEAR_PREV)) {
			final int lastIndexOfprev = URI.lastIndexOf("/prev");
			final String year = URI.substring(6, lastIndexOfprev);
			final String beginDate = "-" + req.getParameter("i");
			hr.CategoryPage(null, year, null, beginDate);
		} else if (URI.matches(YEAR_NEXT)) {
			final int lastIndexOfnext = URI.lastIndexOf("/next");
			final String year = URI.substring(6, lastIndexOfnext);
			final String beginDate = req.getParameter("i");
			hr.CategoryPage(null, year, null, beginDate);
		} else if (URI.matches(AREA)) {
			final String area = URI.substring(6);
			hr.CategoryPage(null, null, area, null);
		} else if (URI.matches(AREA_PREV)) {
			final int lastIndexOfprev = URI.lastIndexOf("/prev");
			final String area = URI.substring(6, lastIndexOfprev);
			final String beginDate = "-" + req.getParameter("i");
			hr.CategoryPage(null, null, area, beginDate);
		} else if (URI.matches(AREA_NEXT)) {
			final int lastIndexOfnext = URI.lastIndexOf("/next");
			final String area = URI.substring(6, lastIndexOfnext);
			final String beginDate = req.getParameter("i");
			hr.CategoryPage(null, null, area, beginDate);
		} else if (URI.matches(SEARCH)) {
			final String q = req.getParameter("q");
			if (null != q) {
				hr.SearchPage(q);
			} else {
				resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
				return;
			}
		} else if (URI.matches("^/actor/(%[0-9a-zA-Z]{2}){6}$")) {
			resp.setStatus(HttpServletResponse.SC_SEE_OTHER);
			return;
		} else if (URI.matches(VIDEO_EDIT)) {
			final int lastIndexOfvideo = URI.lastIndexOf("/video/");
			final String category = URI.substring(6, lastIndexOfvideo);
			final String title = URI.substring(lastIndexOfvideo + 7);
			final String dispatcher = "/admin.do?type=VideoEdit&category="
					+ category + "&title=" + title;
			try {
				req.getRequestDispatcher(dispatcher).forward(req, resp);
			} catch (ServletException e) {
				e.printStackTrace();
			}
			return;
		} else if (URI.matches(VIDEO_DELETE)) {
			final int lastIndexOfvideo = URI.lastIndexOf("/video/");
			final String category = URI.substring(8, lastIndexOfvideo);
			final String title = URI.substring(lastIndexOfvideo + 7);
			final String dispatcher = "/admin.do?type=VideoDelete&category="
					+ category + "&title=" + title;
			try {
				req.getRequestDispatcher(dispatcher).forward(req, resp);
			} catch (ServletException e) {
				e.printStackTrace();
			}
			return;
		} else {
			resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
			return;
		}

		resp.setContentType("text/html");
		resp.setCharacterEncoding("utf-8");
		resp.setHeader("Cache-Control", "public, max-age=600");
		resp.getWriter().print(hr.Render());

	}

	@Override
	public void doPost(final HttpServletRequest req,
			final HttpServletResponse resp) throws IOException {
		resp.sendError(HttpServletResponse.SC_FORBIDDEN);
	}
}
